import $ from '../common';
var appInstance = getApp();
Page({
  data: {
    islogin: false,
    isfirst: false,//是否第一次发送验证码 不是第一次提示内容变成重新发送 第一次是获取验证码
    phone: "",//手机号
    verify: '',//验证码
    password: '',//密码

    // height: "",
    rootNum: 0, //点击图片切换
    areaCode: '+86', //手机所属地区
    // canIUseGetUserProfile: false,
    extraData: {},
    active: 0, //当前未登录的所在索引
    appid: $.appid,
    encryptedData: '', //wx登录的参数 没用
    activeImg: 'https://sxzimgs.oss-cn-shanghai.aliyuncs.com/yingxiao/page/47821c8e-93b9-11ec-a987-00163e04cc20.png',
    btnList: [{
        txt: '首页',
        nIcon: 'https://sxzimgs.oss-cn-shanghai.aliyuncs.com/yingxiao/page/5447c282-9451-11ec-a987-00163e04cc20.png',
        hIcon: 'https://sxzimgs.oss-cn-shanghai.aliyuncs.com/yingxiao/page/55a062ec-9451-11ec-a987-00163e04cc20.png',
        imgSrc: 'https://sxzimgs.oss-cn-shanghai.aliyuncs.com/yingxiao/page/47821c8e-93b9-11ec-a987-00163e04cc20.png'
      },
      {
        txt: '班级',
        nIcon: 'https://sxzimgs.oss-cn-shanghai.aliyuncs.com/yingxiao/page/503c59fa-9451-11ec-a987-00163e04cc20.png',
        hIcon: 'https://sxzimgs.oss-cn-shanghai.aliyuncs.com/yingxiao/page/5253c73c-9451-11ec-a987-00163e04cc20.png',
        imgSrc: 'https://sxzimgs.oss-cn-shanghai.aliyuncs.com/yingxiao/page/6bf87c48-93b9-11ec-a987-00163e04cc20.png'
      },
      {
        txt: '学员',
        nIcon: 'https://sxzimgs.oss-cn-shanghai.aliyuncs.com/yingxiao/page/6135bee0-9451-11ec-a987-00163e04cc20.png',
        hIcon: 'https://sxzimgs.oss-cn-shanghai.aliyuncs.com/yingxiao/page/6aa56516-9451-11ec-a987-00163e04cc20.png',
        imgSrc: 'https://sxzimgs.oss-cn-shanghai.aliyuncs.com/yingxiao/page/8d3837ae-93b9-11ec-a987-00163e04cc20.png'
      },
      {
        txt: '我的',
        nIcon: 'https://sxzimgs.oss-cn-shanghai.aliyuncs.com/yingxiao/page/5f2f9ba2-9451-11ec-a987-00163e04cc20.png',
        hIcon: 'https://sxzimgs.oss-cn-shanghai.aliyuncs.com/yingxiao/page/607b7968-9451-11ec-a987-00163e04cc20.png',
        imgSrc: 'https://sxzimgs.oss-cn-shanghai.aliyuncs.com/yingxiao/page/9ee9b87e-93b9-11ec-a987-00163e04cc20.png'
      }
    ],
    $Reload_page: '', //跳转登录
    token: '',
    iscode: true, //切换微信密码
  },
  //头像的点击
  hideLoginImg() {
    this.setData({
      islogin: true
    })
  },
  //切换账号密码登录
  switchCodeOrPhone() {
    const {
      iscode,
      verify,
      password
    } = this.data
    this.setData({
      iscode: !iscode,
      verify: '',
      password: ''
    })
  },
  tapBtn(e) { //未登录的tabar的点击切换显示图片
    let index = e.currentTarget.dataset.index
    this.setData({
      active: index,
      activeImg: this.data.btnList[index].imgSrc
    })
  },
  changeRoot() { //点击图片切换环境
    this.data.rootNum++
    if (this.data.rootNum > 5) {
      $.navigateTo("/person/about")
    }
  },
  submit(e) { //登录
    let value = e.detail.value
    const {
      iscode,
      phone,
      verify,
      password
    } = this.data
    if (iscode) {
      if (!value.phone || !value.verify) {
        $.tip("手机号或验证码不能为空！")
        return false
      }
    } else {
      if (!value.phone || !value.password) {
        $.tip("手机号或密码不能为空！")
        return false
      }
    }
    if (this.data.areaCode == '+86') {
      this.setData({
        phoneNumber: this.data.phone.replace(/\s/g, "")
      })
    } else {
      this.setData({
        phoneNumber: this.data.areaCode + this.data.phone.replace(/\s/g, "")
      })
    }
    let datainfo = {
      apply: 'TEACHER_SIDE',
      version: 'I-2.1.8',
      phone: phone
    }
    if (iscode) {
      datainfo.verify = verify
      datainfo.passwd = ''
    } else {
      datainfo.verify = ''
      datainfo.passwd = password
    }
    // return
    $.post("/user/login", datainfo, (data) => {
      wx.removeStorageSync('$alreadyLoginWindow')
      wx.setStorageSync('token', data.token)
      // wx.setStorageSync('uuid', data.uuid)
      wx.setStorageSync('phone', this.data.phoneNumber)
      wx.setStorageSync('$Reload_page', "yes")
      let pull = $.parent().onPullDownRefresh
      pull && pull()
      if (this.data.type == "open") {
        wx.navigateBack()
      } else {
        wx.switchTab({
          url: '/pages/index/index'
        })
      }
    })
  },
  bindPhone(e) { //获取输入的手机号
    this.data.phone = e.detail.value.replace(/\s/g, "")
    var phone = this.data.phone
    this.setData({
      phone
      //phone: phone.replace(/(^(\d{3})|(\d{4}))(?=[^\s])/g,'$1 ')
    })
  },
  bindCode(e) { //获取输入短信验证码
    this.setData({
      verify: e.detail.value
    })
  },
  bindPass(e) { //获取输入密码
    this.setData({
      password: e.detail.value
    })
  },
  resetphone() { //清除输入的账号
    this.data.phone = ""
    this.setData({
      phone: ""
    })
  },
  countdown: 60,
  resetDentify() { //发送失败后
    clearInterval(this.setTimeDentify)
    this.disabledCode = false
    this.countdown = 60
    this.setData({
      countdown: this.countdown,
      disabledCode: this.disabledCode
    })
  },
  getIdentify() { //发送验证码
    if (this.data.areaCode == '+86') {
      this.setData({
        phoneNumber: this.data.phone.replace(/\s/g, "")
      })
    } else {
      this.setData({
        phoneNumber: this.data.areaCode + this.data.phone.replace(/\s/g, "")
      })
    }
    $.get("/verifycode", {
      phone: this.data.phoneNumber,
      type: 'LOGIN'
    }, (data) => {
      $.tip("验证码已发送到尾号为" + (this.data.phone).replace(/\s/g, "").toString().substring(7) + "的手机上，请注意查收!")
    }, (res) => {
      $.tip(res.message)
      this.resetDentify()
    })
  },
  getCode() { //发送请求获取验证码
    console.log('点击的时候调用发送请求和定时器');
    if (!this.data.phone) {
      $.tip("手机号不能为空！")
      return false
    }
    let countdown = this.countdown
    let _fn = () => {
      if (countdown == 0) {
        this.resetDentify()
      } else {
        this.disabledCode = true
        this.setData({
          countdown: countdown,
          disabledCode: this.disabledCode
        })
        if (countdown == 60) {
          this.getIdentify()
        }
        countdown--
      }
    }
    _fn()
    this.setTimeDentify = setInterval(() => {
      _fn()
    }, 1000)
  },
  onShareAppMessage(res) { //转发
    return {
      title: '移动办公，老师更轻松！',
      path: '/pages/index/index',
      imageUrl: 'https://sxzimgs.oss-cn-shanghai.aliyuncs.com/yingxiao/page/b827c6da-ac0c-11ec-a9c2-00163e04cc20.png'
    }
  },
  onLoad(e) {
    wx.setNavigationBarColor({
      frontColor: '#000000',
      backgroundColor: '#ffffff'
    })
    wx.hideLoading()
    this.data.type = e.type
    this.data.$Reload_page = wx.getStorageSync('$Reload_page')
    this.setData({
      $Reload_page: this.data.$Reload_page
    })
  },
  onShow() {
    this.data.extraData = appInstance.globalData.extraData
  },
  select() { //跳转到选手机号所在区域
    wx.navigateTo({
      url: '../../packageA/login/selectcode',
    })
  },
})